package com.rd.student.dashuju.zhengruixian;

import java.util.*;

public class Paixu {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        Student s1 = new Student("liusan", 20, 90);
        Student s2 = new Student("lisi", 22, 90);
        Student s3 = new Student("wangwu", 20, 99);
        Student s4 = new Student("sunliu", 22, 100);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        Collections.sort(list, new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getGrade() == o2.getGrade())
                    return o2.getAge() - o1.getAge();
                return o1.getGrade() - o2.getGrade();
            }
        }

        );

        Collections.sort(list);
        for (Student student:list) {
            System.out.println(student);
        }
    }

    }
   class Student implements Comparable<Student>{
        private String name;
        private int age;
        private int grade;
        public Student(){}

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public int getGrade() {
            return grade;
        }

        public void setGrade(int grade) {
            this.grade = grade;
        }
       @Override
       public String toString(){
           return "[name:"+name+"age:"+age+"grade:"+grade+"]";

       }

        public Student(String name, int age, int grade){
            this.name=name;
            this.age=age;
            this.grade=grade;

        }

        @Override
        public int compareTo(Student o){
            if (this.grade>o.getGrade()){
                return -1;
            }
            if (this.grade<o.getGrade()){
                return 1;
            }
            return 0;
        }
    }
